import Vue from 'vue'
import Vuex from 'vuex'
import SockJS from 'sockjs-client';
import Stomp from 'stompjs';
import { Notice } from 'view-design'
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        stompclient:null,
        userMsg:null || JSON.parse(sessionStorage.getItem("userMsg")),
        taskMsg:null || JSON.parse(sessionStorage.getItem("taskMsg"))
    },
    mutations: {
        startsocket(state,val){
            Notice.config({top: 70});
            if (state.stompclient) {
                state.stompclient.disconnect()
            }
            let socketjs = new SockJS('/api/backstage/pcSocket?userId='+val,null, { timeout: 15000})
          //let socketjs = new SockJS('http://127.0.0.1:8216/pcSocket?userId='+val)
            state.stompclient = Stomp.over(socketjs)
            state.stompclient.debug = false
            state.stompclient.connect({"token":val,"Authorization":val},()=>{
                state.stompclient.subscribe("/user/topic/userMsg",(data)=>{
                    state.userMsg = JSON.parse(data.body)
                    state.userMsg.filter(r => !r.isRead).forEach(f => {
                        Notice.info({
                            title: '公告',
                            duration:0,
                            render: h => {
                                return <div style="line-height:30px">
                                    <p>{new Date(f.updateTime).toLocaleDateString()}</p>
                                    <p><strong>{f.title}</strong></p>
                                </div>
                            }
                        });
                    })
                    sessionStorage.setItem("userMsg",  data.body)
                })
                state.stompclient.subscribe("/user/task/userTask",(data)=>{
                    state.taskMsg = JSON.parse(data.body)
                    state.taskMsg.filter(r => !r.isRead).forEach(f => {
                        Notice.warning({
                            title: '任务',
                            duration:0,
                            render: h => {
                                return <div style="line-height:30px">
                                    <p>{new Date(f.startTime).toLocaleDateString()}</p>
                                    <p><strong>{f.taskName}</strong></p>
                                </div>
                            }
                        });
                    })
                    sessionStorage.setItem("taskMsg",  data.body)
                })
            })
        },
        sendusermsg(state,val){
            state.stompclient.send("/mysocket/userMsg",{},val)
        },
        updateAnnouncement(state,val){
            state.userMsg = state.userMsg.filter(f=>f.announcementId!=val)
            let str = JSON.stringify(state.userMsg)
            sessionStorage.setItem("userMsg", str)
        },
        postTask(state,val){
            state.stompclient.send("/mysocket/postTask",{},val)
        }
    },
    actions: {

    },
    modules: {
    }
})
